package cn.wolfcode.service.impl;

import cn.wolfcode.domain.*;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.query.StrategyQueryObject;
import cn.wolfcode.repsitory.StrategyRepsitory;
import cn.wolfcode.service.*;
import javafx.fxml.LoadException;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description: Strategy业务逻辑实现类
 * @Author: 伍家钰
 * @Date: 2020/8/7 16:20
 * @Version: 1.0
 */
@Service
public class StrategyServiceImpl implements IStrategyService {
    @Resource
    private StrategyRepsitory strategyRepsitory;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private IStrategyCatalogService strategyCatalogService;
    @Resource
    private IDestinationService destinationService;
    @Resource
    private IStrategyThemeService strategyThemeService;
    @Resource
    private ITravelService travelService;

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        strategyRepsitory.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepsitory.deleteById(id);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepsitory.save(strategy);
    }

    @Override
    public Strategy get(String id) {
        return strategyRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return null;
    }


    @Override
    public Page<Strategy> query(StrategyQueryObject qo) {
        Query query = new Query();
        if (StringUtils.hasText(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasText(qo.getThemeId())) {
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        // 当前页显示的数据,Pageable是PageRequest的父接口的父接口
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageable);
        // 总数据
        long total = mongoTemplate.count(query, Strategy.class);
        if (total == 0) {
            // 如果返回null会报错
            return Page.empty();
        }
        List<Strategy> list = mongoTemplate.find(query, Strategy.class);
        return new PageImpl<>(list, pageable, total);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        /*
         *由于保存有些数据不用我们保存和修改,在保存时如果这些数据没值会存在数据丢失的情况,
         * 此时需要查询出数据在设置进对象中
         */
        // 根据catalogId获取到StrategyCatalog对象
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
        // 2.获取destId和destName的值设置给strategy
        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        // 3.设置cataloggName的值
        strategy.setCatalogName(catalog.getName());
        // 4.设置创建时间
        strategy.setCreateTime(new Date());
        // 5.判断是国内还是国外并且设置值
        List<Destination> destinations = destinationService.queryToast(catalog.getDestId());
        Destination destination = destinations.get(0);
        // 获取地区的名字
        String name = destination.getName();
        // 设置是国内还是国外
        strategy.setIsabroad("中国".equals(name) ? Strategy.ABROAD_NO : Strategy.ABROAD_YES);
        // 6.设置主题名称
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        String themeName = strategyTheme.getName();
        strategy.setThemeName(themeName);
        if (StringUtils.hasText(strategy.getId())) {
            // 编辑
            // 设置点击数
            Strategy strategy1 = this.get(strategy.getId());
            strategy.setViewnum(strategy1.getViewnum());
            strategy.setReplynum(strategy1.getReplynum());
            strategy.setFavornum(strategy1.getFavornum());
            strategy.setSharenum(strategy1.getSharenum());
            strategy.setThumbsupnum(strategy1.getThumbsupnum());
            this.update(strategy);
        } else {
            // 保存
            this.save(strategy);
        }
    }

    @Override
    public List<Strategy> findByCatalogId(String catalogId) {
        return strategyRepsitory.findByCatalogId(catalogId);
    }

    @Override
    public List<Strategy> queryViewnumTop3(String destId) {
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        return strategyRepsitory.findByDestId(destId, pageable);
    }
}
